package com.itwelkin.producer;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:rabbitmq_producer.xml")
public class ProducerDemo1 {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void demo1() {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                // 配置信息；如果执行成功返回true，执行失败返回false；失败原因
                System.out.println(correlationData);
                System.out.println(b);
                System.out.println(s);
            }
        });

//        rabbitTemplate.convertAndSend("exchange_confirm","routing_key_confirm","hello, 刘昊然"); // 消息丢失：交换机名称错误
        rabbitTemplate.convertAndSend("exchange_confirm1","routing_key_confirm","hello, 刘昊然");
    }

    @Test
    public void demo2() {
        rabbitTemplate.setMandatory(true); // 开启受托授权
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                // 错误对象；错误代码；错误描述；交换机信息；路由键信息
                System.out.println(message);
                System.out.println(i);
                System.out.println(s);
                System.out.println(s1);
                System.out.println(s2);
            }
        });
        rabbitTemplate.convertAndSend("exchange_confirm","routing_key_confirm2","hello, 刘昊然");
    }

    @Test
    public void demo3() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("exchange_confirm","routing_key_confirm",i+"hello, 刘昊然");
        }
    }

    @Test
    public void demo4() {
        // 消息ttl设置
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000"); // 拦截后，设置参数：时间
                return message;
            }
        };
        rabbitTemplate.convertAndSend("exchange_confirm","routing_key_confirm","hello, 刘昊然",messagePostProcessor);
//        for (int i = 0; i < 10; i++) {
//            if(i == 5){
//                rabbitTemplate.convertAndSend("exchange_confirm","routing_key_confirm",i+"hello, 刘昊然",messagePostProcessor);
//            }else{
//                rabbitTemplate.convertAndSend("exchange_confirm","routing_key_confirm",i+"hello, 刘昊然");
//            }
//        }
    }

    @Test
    public void demo5() {
        for (int i = 0; i < 5; i++) {
            rabbitTemplate.convertAndSend("exchange_dlx","message.dlx","晚上好, 刘昊然");
        }
    }

}
